import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class Test {
    public static void main(String[] args) {
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>();

        Thread consumer = new Thread(()->{
            try {
                while (true) {
                    int m = blockingDeque.take();
                    System.out.println("消费了一个元素：" + m);
                }

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"消费者");
        consumer.start();

        Thread producer = new Thread(()->{
            Random random = new Random();
            try {
                while (true) {
                    int n = random.nextInt(1000);
                    blockingDeque.put(n);
                    System.out.println("生产了一个元素：" + n);
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"生产者");
        producer.start();
    }
}
